//
//  ReportCardProblemListVC.m
//  jzyParents
//
//  Created by 任辰东 on 2022/8/22.
//

#import "ReportCardProblemListVC.h"
#import "ReportCardProblemTCell.h"
#import "ReportCardProblemHeaderView.h"
#import "ReportApis.h"
#import "ReportCradAnswerSheetListVC.h"
#import "ReportCardDetailPageVC.h"

@interface ReportCardProblemListVC ()

@property (nonatomic, strong) ReportCardProblemHeaderView *headerView;

@property (nonatomic, strong) ReportCardModel *mainModel;
@property (nonatomic, strong) ReportCardKemuModel *kemuModel;

@property (nonatomic, strong) ReportCardKemuDetailModel *detailModel;
@end

@implementation ReportCardProblemListVC

-(instancetype)initWithMainModel:(ReportCardModel *)mainModel WithKemuModel:(ReportCardKemuModel *)kemuModel
{
    self = [super init];
    if (self) {
        _mainModel = mainModel;
        _kemuModel = kemuModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"查看";
    
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.view);
    }];
    
    [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
         
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(kBottomOffset);
    }];
    
    [self setupEstimatedRowHeight:120 cellClasses:@[[ReportCardProblemTCell class]]];
    [self beginRefresh];
    
    self.myTableView.mj_footer = nil;
}

-(void)requestDataWithOffset:(NSInteger)offset success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"main_id"] = self.mainModel.main_id;
    params[@"kemu_id"] = self.kemuModel.kemu_id;
    params[@"id_card"] = [GVUserDefaults standardUserDefaults].id_card;
//    params[@"main_id"] = @"333";
//    params[@"kemu_id"] = @"768";
//    params[@"id_card"] = @"410802200912100020";
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [ReportApis ctb_Xq_DankeChengjiPageRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        ReportCardKemuDetailModel *detailModel = [ReportCardKemuDetailModel mj_objectWithKeyValues:responseData];
        self.detailModel = detailModel;
        NSString *title = [NSString stringWithFormat:@"%@--%@  得分  %@分",[GVUserDefaults standardUserDefaults].class_name,[GVUserDefaults standardUserDefaults].student_name,IsEmpty(detailModel.zongdefen)?@"":detailModel.zongdefen];
        [self.headerView configViewWithTitle:title];
        success(detailModel.list);
        [self addEmptyView];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Xiaoti_score_tongjiItem *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }
    
    if (model.isOpen) {
        
        model.isOpen = NO;
        [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } else {
        
        if (IsEmpty(model.junfen)) {
            [self getJunFenDataRowAtIndexPath:indexPath];
        }else{
            model.isOpen = YES;
            [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
}

-(void)getJunFenDataRowAtIndexPath:(NSIndexPath *)indexPath
{
    Xiaoti_score_tongjiItem *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"banji_id"] = [GVUserDefaults standardUserDefaults].class_id;
    params[@"id_card"] = [GVUserDefaults standardUserDefaults].id_card;
    params[@"ti_id"] = model.ti_id;
//    params[@"banji_id"] = @"490";
//    params[@"id_card"] = @"410802200912100020";
    
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [ReportApis getStuPingjunfenRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        model.isOpen = YES;
        Xiaoti_junfenModel *smodel = [Xiaoti_junfenModel mj_objectWithKeyValues:responseData];
        NSString *junfen = [NSString stringWithFormat:@"班级%@分，年级%@分，联考%@分",smodel.banji_junfen,smodel.organ_junfen,smodel.all_junfen];
        
        model.junfen = junfen;
        [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
    
}

-(void)addEmptyView
{
    if (IsEmpty(self.dataSourceArr)) {
        [RCDEmptyView addEmptyWithStyle:EmptyViewStyleCommon onView:self.myTableView position:EmptyViewPositionCenter clickCallBack:nil];
    }else{
        [RCDEmptyView removeFromParentView:self.myTableView];
    }
}

-(ReportCardProblemHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [ReportCardProblemHeaderView new];
    }
    return _headerView;
}

@end
